// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { CharacterType } from "../../Enums/GameEnum";
import { UINames } from "../../Enums/UIEnum";
import AudioManager from "../../Managers/AudioManager";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import CharacterManager, { CharacterEvent } from "../../Models/Characters/CharacterManager";
import CharacterTemplate from "../../Models/Characters/CharacterTemplate";
import GiftManager, { GiftEvent } from "../../Models/Gifts/GiftManager";
import { SkillEvent } from "../../Models/Skills/SkillManager";
import BaseUI from "../BaseUI";
import HeroItemUi from "../UIItems/HeroItemUi";
import MovieClipUI from "./MovieClipUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MenuUI extends cc.Component {

    @property(cc.ScrollView)
    heroScrollView: cc.ScrollView = null;
    @property(cc.Prefab)
    heroItemUiPrefab: cc.Prefab = null;
    @property([cc.Toggle])
    toggleArr: cc.Toggle[] = [];
    @property([cc.Node])
    uiArr: cc.Node[] = [];
    @property(MovieClipUI)
    movieClipUi: MovieClipUI = null;
    @property(cc.Node)
    guideHand: cc.Node = null;
    @property(cc.Node)
    menuToggleContainer: cc.Node = null;
    @property(cc.Node)
    currencyComp: cc.Node = null;
    @property(cc.Node)
    mapBtn: cc.Node = null;
    @property(cc.Node)
    fubenBtn: cc.Node = null;
    @property(cc.Node)
    giftNode: cc.Node = null;
    @property(cc.Node)
    exitFubenNode: cc.Node = null;

    currentUI: cc.Node = null;

    protected onLoad(): void {
        this.exitFubenNode.active = false;
        for (let i = 0; i < 10; i++) {
            var itemNode = cc.instantiate(this.heroItemUiPrefab);
            itemNode.active = false;
            itemNode.y = 0;
            this.heroScrollView.content.addChild(itemNode);
        }

        document.addEventListener("heartbeatResult", (event: CustomEvent) => {
            // console.log('心跳结果', event);
            if (event.detail.message.code == 403) {

                // UIManager.single.showDialog("玩家为未成年，已停止游戏，即将关闭游戏", () => {
                //     cc.game.end();
                // });

                UIManager.single.showUI(UINames.NotIndulgeTipsUI);
                this.scheduleOnce(() => {
                    // cc.game.pause();
                    window.location.reload();
                }, 5);

            }
        });

        this.schedule(() => {
            SaveManager.single.gameDataReport();
        }, 30);
    }

    protected onEnable(): void {
        this.drawHeros();
        this.onBuyGift();
        GameEvent.eventNode.on(CharacterEvent.UPGRADE_EVENT, this.onUpgrade, this);
        GameEvent.eventNode.on(CharacterEvent.UNLOCKHERO_EVENT, this.onUnlockHero, this);
        GameEvent.eventNode.on(SkillEvent.SKILL_UPGRADE_EVENT, this.drawHeros, this);
        GameEvent.eventNode.on(GameEvent.UPDATAGUIDE_EVENT, this.onUpdataGuide, this);
        GameEvent.eventNode.on(GameEvent.STARTFUBEN_EVENT, this.OnStartFuBen, this);
        GameEvent.eventNode.on(GameEvent.ENDFUBEN_EVENT, this.OnEndFuBen, this);
        GameEvent.eventNode.on(GiftEvent.BUYGIFT_EVENT, this.onBuyGift, this);

        if (this.currentUI != null) {
            this.currentUI.active = false;
        }
        this.toggleArr[2].isChecked = true;
        AudioManager.single.playBgm();

        if (!SaveManager.single.userdata.isPlayMovie) {
            this.movieClipUi.node.active = true;
        } else {
            this.movieClipUi.node.active = false;
        }
        this.scheduleOnce(() => {
            if (SaveManager.single.userdata.guideId > 2) {
                this.guideHand.active = false;
            }
        }, 0);

    }
    protected onDisable(): void {
        GameEvent.eventNode.off(CharacterEvent.UPGRADE_EVENT, this.onUpgrade, this);
        GameEvent.eventNode.off(CharacterEvent.UNLOCKHERO_EVENT, this.onUnlockHero, this);
        GameEvent.eventNode.off(SkillEvent.SKILL_UPGRADE_EVENT, this.drawHeros, this);
        GameEvent.eventNode.off(GameEvent.UPDATAGUIDE_EVENT, this.onUpdataGuide, this);
        GameEvent.eventNode.off(GameEvent.STARTFUBEN_EVENT, this.OnStartFuBen, this);
        GameEvent.eventNode.off(GameEvent.ENDFUBEN_EVENT, this.OnEndFuBen, this);
        GameEvent.eventNode.off(GiftEvent.BUYGIFT_EVENT, this.onBuyGift, this);

        for (const iterator of this.heroScrollView.content.children) {
            iterator.active = false;
        }
        AudioManager.single.stopBgm();
    }
    onBuyGift() {
        // if (CC_BUILD) {
        this.giftNode.active = !GiftManager.single.isAllBuyGift();
        // }

    }
    OnStartFuBen() {
        this.menuToggleContainer.active = false;
        this.currencyComp.active = false;
        this.mapBtn.active = this.fubenBtn.active = false;
        this.exitFubenNode.active = true;
    }
    OnEndFuBen() {
        this.menuToggleContainer.active = true;
        this.currencyComp.active = true;
        this.mapBtn.active = this.fubenBtn.active = true;
        this.exitFubenNode.active = false;
    }
    onUpdataGuide() {
        this.drawHeros();
    }
    onUnlockHero() {
        this.drawHeros();
    }
    onUpgrade() {
        this.drawHeros();
    }

    drawHeros() {
        var tps = CharacterManager.single.getAllCharacters(CharacterType.player);
        let arr: CharacterTemplate[] = [];
        let arr2: CharacterTemplate[] = [];
        for (const iterator of tps) {
            if (CharacterManager.single.getCharacteEntity(iterator.id) != null) {
                arr.push(iterator);
            } else {
                arr2.push(iterator);
            }
        }
        arr = arr.concat(arr2);
        var userdata = SaveManager.single.userdata;
        for (let i = 0; i < arr.length; i++) {
            var itemNode = this.heroScrollView.content.children[i];
            if (itemNode == null) {
                itemNode = cc.instantiate(this.heroItemUiPrefab);
                this.heroScrollView.content.addChild(itemNode);
            }
            itemNode.active = true;
            var script = itemNode.getComponent(HeroItemUi);
            script.drawItem(arr[i]);
            if (userdata.guideId == 2 && i == 0) {
                if (this.guideHand.parent != null) this.guideHand.removeFromParent();
                this.guideHand.setPosition(123, -40);
                this.guideHand.getChildByName("txt").getComponent(cc.Label).string = "点击升级";
                itemNode.addChild(this.guideHand);
            }
        }

    }

    toggleClick(toggle, index: string) {
        if (this.currentUI != null) {
            this.currentUI.active = false;
        }
        if (index == "3") {
            return;
        }
        this.currentUI = this.uiArr[Number(index) - 1];
        this.currentUI.active = true;
        // switch (index) {
        //     case "1":
        //         UIManager.single.showUI(UINames.SkillUI);
        //         break;
        //     case "2":
        //         UIManager.single.showUI(UINames.EquipmentUI);
        //         break;
        //     case "4":
        //         UIManager.single.showUI(UINames.PetUI);
        //         break;
        //     case "5":
        //         UIManager.single.showUI(UINames.ShopUI);
        //         break;
        // }
    }
}
